const Tool = {
  /*1.空值null/""校验，返回true
  !:将变量转为boolean类型，!(obj)=false
  数字注意：number=true !number=false !!number=true  (number是数字，返回true，!number不是数字，返回false)
  字符串注意：string=true !string=false !!string=true (同上)
  对象注意：{}=true !{}=false !!{}=true
  null=false !null=true !!null=false
  undefined=false !undefined=true !!undefined=false

   */
  isEmpty: function (obj) {
      if (typeof obj === 'string') {
          //如果为string，去空格，变为空. obj=string 返回false
          return !obj || !obj.replace(/\s+/g, "");
      } else {
          //转obj为字符串str
          return (!obj || JSON.stringify(obj) === "{}" || !obj.length === 0);
      }
  },
  //2.非空校验
  isNotEmpty: function (obj) {
      //非空，对于空取反，全部返回true
      return !this.isEmpty(obj);
  },

  //4.增加日期格式化方法
  dateFormat: function (format, date) {
      let result;
      if (!date) {
          date = new Date();
      }
      const option = {
          "y+": date.getFullYear().toString(),
          "M+": (date.getMonth() + 1).toString(), //月份需要加1
          "d+": date.getDate().toString(), //day为一周的第几天，date为一个月的第几天
          "h+": date.getHours().toString(),
          "m+": date.getMinutes().toString(),
          "s+": date.getSeconds().toString()
      };
      for (let i in option) {
          result = new RegExp("(" + i + ")").exec(format);
          if (result) {
              format = format.replace(result[1], (result[1].length == 1) ? (option[i]) : (option[i].padStart(result[1].length, "0")));
          }
      }
      return format;
  },
  /*前端日期*/
  dateWeb: function () {
      let time = new Date();
      let year = time.getFullYear();
      let month = time.getMonth();
      let day = time.getDate();
      let hour = time.getHours();
      let minute = time.getMinutes();
      let second = time.getSeconds();
      month = month < 10 ? '0' + month : month;
      day = day < 10 ? '0' + day : day;
      hour = hour < 10 ? '0' + hour : hour;
      minute = minute < 10 ? '0' + minute : minute;
      second = second < 10 ? '0' + second : second;
      return year + '-' + month + '-' + day + ' ' + hour + ":" + minute + ":" + second;
  },
  //5.移除数组中的对象
  removeObj: function (array, obj) {
      let index = -1;
      for (let i = 0; i < array.length; i++) {
          if (array[i] === obj) {
              array.splice(i, 1);
              index = i;
              break;
          }
      }
      return index;
  },
  //6.md5转换10进制转62进制
  md_10to64: function (number) {
      let chars = '0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ';
      let radix = chars.length;//基数长度
      let arr = [];
      do {
          let mode = number % radix;
          number = (number - mode) / radix;
          arr.unshift(chars[mode]);
      } while (number);
      return arr.join('');
  },

  /**9.随机八位字符串生成imgCodeToken
   * 随机生成[length]长度的[radix]（基数）进制数,radix默认62(10个数字26个大写26个小写)
   * 随机生成0-61的数值，8位，重复循环8次，重复概率为62的8次方
   * 如果怕重复，可调整length
   */
  get8UUID: function (length, radix) {
      let char = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
      let uuid = [];
      radix = radix || char.length;
      for (let i = 0; i < length; i++) {
          uuid[i] = char[0 | Math.random() * radix];
      }
      return uuid.join('');
  },
  /**
   * 获取登录emp资源权限
   */
  authorityResources: function (uniId) {
      //获取employeeLoginDto中缓存的List<resourcesDto>资源数据
      let resources = this.getEmpLogin().resourcesDtos;
      if (this.isEmpty(resources)) {
          return false;
      }
      for (let i = 0; i < resources.length; i++) {
          if (uniId === resources[i].uniId) {
              return true;
          }
      }
      return false;
  },
  /*添加删除class*/
  addClass(node, className) {
      let reg = new RegExp("\\b" + className + "\\b");
      if (!reg.test(node.className)) {
          node.className += (" " + className);
      }
  },
  removeClass(node, className) {
      if (node.className) {
          let reg = new RegExp("\\b" + className + "\\b");
          let classes = node.className;
          node.className = classes.replace(reg, "");
          if (/^\s*$/g.test(node.className)) {
              node.removeAttribute("class");
          }
      } else {
          node.removeAttribute("class");
      }
  },
  /** toggleClass可以用来切换一个类
   *    如果元素中具有该类，则删除
   *    如果元素中没有该类，则添加
   */
  hasClass(obj, className) {

      //判断obj中有没有cn class
      //创建一个正则表达式
      //var reg = /\bb2\b/;
      let reg = new RegExp("\\b" + className + "\\b");
      return reg.test(obj.className);

  },
  toggleClass(obj, className) {
      //判断obj中是否含有cn
      if (this.hasClass(obj, className)) {
          //有，则删除
          this.removeClass(obj, className);
      } else {
          //没有，则添加
          this.addClass(obj, className);
      }

  },
  
  //跳转新标签页
  toLink(url){
      let a=document.createElement("a");
      a.setAttribute("href",url);
      a.setAttribute("target","_blank");
      a.click();
  },


  /*tree型数据过滤*/
  filterTree(data) {
      for (let i = 0; i < data.length; i++) {
          if (data[i].children.length < 1) {
              data[i].children = undefined;
          } else {
              this.filterTree(data[i].children);
          }
      }
      return data;
  },
  /*类型id转换value*/
  optionObjectKV(object, key) {
      console.log("传入得对象：", object,"传入的key：",key);
      //传入非list，非key,undefined=空,这里不能使用!key，非key会连同0一起判定，这里我们有type=0，使用key=null
      let KV = "";
      for (let enums in object) {
          console.log("enumsFilter传入的enums: ",enums,"-----objKey: ",object[enums].key);
          if(object[enums].key===key){
              KV=object[enums].value;
              console.log("KV入值：",KV);
          }
      }
      return KV;

  },
}

export default Tool